<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div class="filter-content">
        <el-form :inline="true" :model="listQuery.params" size="small">
          <el-form-item label="考试名称">
            <el-input v-model="listQuery.params.title" placeholder="请输入考试名称" clearable /> 
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetFilter">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-card class="list-container" shadow="never">
      <data-table
        ref="pagingTable"
        :options="options"
        :list-query="listQuery"
      >
        <template #data-columns>
          <el-table-column label="考试名称" prop="title" show-overflow-tooltip />

          <el-table-column label="考试次数" prop="tryCount" align="center" />

          <el-table-column label="最高分" prop="maxScore" align="center" />

          <el-table-column label="是否通过" align="center">
            <template v-slot="scope">
              <span v-if="scope.row.passed" style="color: #00ff00;">通过</span>
              <span v-else style="color: #ff0000;">未通过</span>
            </template>
          </el-table-column>

          <el-table-column label="最后考试时间" prop="updateTime" align="center" />

          <el-table-column label="操作" align="center">
            <template v-slot="scope">
              <el-button 
                type="primary" 
                size="mini" 
                icon="el-icon-view" 
                @click="handleViewDetail(scope.row)">详情
              </el-button>
            </template>
          </el-table-column>
        </template>
      </data-table>
    </el-card>

    <el-dialog :visible.sync="dialogVisible" title="考试明细" width="60%">
      <div class="el-dialog-div">
        <my-paper-list :exam-id="currentExamId" :user-id="currentUserId" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import DataTable from '@/components/DataTable'
import MyPaperList from '@/views/user/exam/paper'

export default {
  name: 'PublicRepoExamRecords',
  components: { DataTable, MyPaperList },
  data() {
    return {
      listQuery: {
        current: 1,
        size: 10,
        params: {
          title: ''
        }
      },
      options: {
        // 列表请求URL
        listUrl: '/exam/api/user/exam/public-repo-paging'
      },
      dialogVisible: false,
      currentExamId: ''
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      // 初始化数据
    },
    handleFilter() {
      this.listQuery.current = 1
      this.$refs.pagingTable.getList()
    },
    resetFilter() {
      this.listQuery.params = {
        title: ''
      }
      this.handleFilter()
    },
    handleViewDetail(row) {
      this.currentExamId = row.examId
      this.dialogVisible = true
    }
  },
  computed: {
    currentUserId() {
      return this.$store.state.user && this.$store.state.user.userId ? this.$store.state.user.userId : ''
    }
  }
}
</script>

<style scoped>
.filter-container {
  margin-bottom: 20px;
}
.el-dialog-div{
  height: 60vh;
  overflow: auto;
  padding: 10px;
}
</style>